package com.banding.LeetCode;

public class Practice169 {
    public static int majorityElement(int[] nums) {
        return majorityElement(nums, 0, nums.length - 1);
    }

    private static int majorityElement(int[] nums, int start, int end) {
        if (start == end) {
            return nums[start];
        }

        int mid = (start + end) / 2;

        int leftMajorityElement = majorityElement(nums, start, mid);
        int rightMajorityElement = majorityElement(nums, mid + 1, end);

        int leftCount = 0;
        int rightCount = 0;

        for (int i = start; i <= end; i++) {
            if (nums[i] == leftMajorityElement) {
                leftCount++;
            }
            if (nums[i] == rightMajorityElement) {
                rightCount++;
            }
        }

        return leftCount > rightCount ? leftMajorityElement : rightMajorityElement;

    }
}
